package com.naiterui.ehp.bs.emr.service.impl;

import cn.hutool.core.map.MapUtil;
import com.google.common.collect.Lists;
import com.naiterui.ehp.bp.bo.patient.PatientInquirerBO;
import com.naiterui.ehp.bp.bo.smh.SmhUserBO;
import com.naiterui.ehp.bp.constants.InquirerRelationEnum;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.emr.feign.IPatientFeignClient;
import com.naiterui.ehp.bs.emr.service.IHisService;
import com.naiterui.ehp.bs.emr.service.ISmhService;
import com.naiterui.ehp.bs.emr.utils.his.HisApi;
import com.naiterui.ehp.bs.emr.utils.his.param.resp.AnalyzeResp;
import com.naiterui.ehp.bs.emr.utils.his.param.resp.CaseResp;
import com.naiterui.ehp.bs.emr.utils.his.param.resp.ExamineResp;
import com.naiterui.ehp.bs.emr.utils.his.param.resp.MedicalResp;
import com.naiterui.ehp.bs.emr.utils.his.param.resp.PrescriptionResp;
import com.naiterui.ehp.bs.emr.vo.openapi.his.AnalyzeRecordVO;
import com.naiterui.ehp.bs.emr.vo.openapi.his.CaseRecordVO;
import com.naiterui.ehp.bs.emr.vo.openapi.his.Examine;
import com.naiterui.ehp.bs.emr.vo.openapi.his.ExamineRecordVO;
import com.naiterui.ehp.bs.emr.vo.openapi.his.MedicalRecordVO;
import com.naiterui.ehp.bs.emr.vo.openapi.his.PrescriptionRecordVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * his业务层
 *
 * @author guoyongxiang
 * @version 1.0.0
 * @date 2021/8/12 18:01
 **/
@Slf4j
@RequiredArgsConstructor
@Service
public class HisServiceImpl implements IHisService {

    private final IPatientFeignClient patientFeignClient;
    private final ISmhService mshService;

    @Override
    public SmhUserBO getPatientList(Long patientId, Long inquirerId) throws BusinessException {
        List<PatientInquirerBO> inquirerBOList;
        if (inquirerId == null) {
            inquirerBOList =
                patientFeignClient.getPatientInquirerByRelation(patientId, InquirerRelationEnum.ONESELF.getValue());
        } else {
            inquirerBOList = patientFeignClient.getPatientInquirer(Lists.newArrayList(inquirerId));
        }
        if (!CollectionUtils.isEmpty(inquirerBOList)) {
            PatientInquirerBO inquirer = inquirerBOList.get(0);
            List<SmhUserBO> smhUserVOS = mshService.getPatientListByIds(MapUtil.builder(inquirer.getIdCard(), inquirer.getName()).map());
            if (!CollectionUtils.isEmpty(smhUserVOS)) {
                return smhUserVOS.get(0);
            }
        }
        return null;
    }

    @Override
    public List<MedicalRecordVO> findMedicalRecord(String idCard, String name) throws BusinessException {
        List<MedicalResp> medicalResps = HisApi.getMedical(idCard, name);
        return medicalResps.stream().sorted(Comparator.comparing(MedicalResp::getVistingTime).reversed())
                .map(MedicalRecordVO::of).collect(Collectors.toList());
    }

    @Override
    public CaseRecordVO getCaseRecord(String mzh) throws BusinessException {
        CaseResp resp = HisApi.getCase(mzh);
        CaseRecordVO vo = new CaseRecordVO();
        BeanUtils.copyProperties(resp, vo);
        return vo;
    }

    @Override
    public List<PrescriptionRecordVO> findPrescriptionRecord(String mzh) throws BusinessException {
        List<PrescriptionResp> resps = HisApi.getPrescription(mzh);
        return resps.stream().map(PrescriptionRecordVO::of).collect(Collectors.toList());
    }

    @Override
    public List<AnalyzeRecordVO> findAnalyzeRecord(String mzh) throws BusinessException {
        List<AnalyzeResp> resps = HisApi.getAnalyze(mzh);
        return resps.stream().map(AnalyzeRecordVO::of).collect(Collectors.toList());
    }

    @Override
    public ExamineRecordVO findExamineRecord(String mzh) throws BusinessException {
        CaseResp caseResp = HisApi.getCase(mzh);
        List<ExamineResp> resps = HisApi.getExamine(mzh);
        return ExamineRecordVO.builder()
                .inquirerName(caseResp.getInquirerName())
                .gender(caseResp.getGender())
                .age(caseResp.getAge())
                .items(resps.stream().map(Examine::of).collect(Collectors.toList()))
                .build();
    }
}
